#include<stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>

#define UNIX_PATH "/tmp/unix_dgram.sock"

    int main(void)
{
    int sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket error:　");
        exit(EXIT_FAILURE);
    }

    unlink(UNIX_PATH);
 

    struct sockaddr_un serverAddr, clientAddr;
    int addrLen = sizeof(serverAddr);
    int caddrLen = sizeof(clientAddr);
    memset(&serverAddr, 0, addrLen);
    serverAddr.sun_family = AF_LOCAL;
    strcpy(serverAddr.sun_path, UNIX_PATH);

    if (bind(sockfd, (struct sockaddr *)&serverAddr, addrLen) == -1)
    {
        perror("bind error: ");
        exit(EXIT_FAILURE);
    }

    while (1)
    {
        char buff[1024] = {0};
        int ret = recvfrom(sockfd, buff, 1024, 0, (struct sockaddr *)&clientAddr, (socklen_t *)&caddrLen);
        if (ret > 0)
        {
            printf("%s\n", buff);
            int val = sendto(sockfd, buff, ret, 0, (struct sockaddr *)&clientAddr, caddrLen);
            if(val < 0)
            {
                perror("sendto: ");
            }
        }
    }

    return 0;
}